package com.ttscm.srds.robot.seer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttscm.srds.config.RobotParam;
import com.ttscm.srds.datagram.SeerDataPacket;
import com.ttscm.srds.pojo.BusinessException;
import com.ttscm.srds.utils.ConnectionUtil;
import com.ttscm.srds.utils.PacketUtil2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version:
 * @author:
 * @className:
 * @packageName:
 * @description: 机器人导航 API
 * @date: 2022/07/20
 */
@Component
public final class RobotTask {
    private final static int ROBOT_PORT = 19206;

    private final static int API_GO_TARGET_3051 = 3051;
    private final static int API_TASK_LIST_NAME = 3106;

    @Autowired
    private RobotParam config;

    /**
     * 纯固定路径导航
     *
     * @param sourceSite
     * @param targetSite
     * @return
     * @throws IOException
     */
    public SeerDataPacket goTarget3051(String sourceSite, String targetSite) throws IOException {
        if (!StringUtils.hasText(targetSite)) {
            throw new BusinessException(999001, "无效的目标站点");
        }
        String sourceSite2 = sourceSite;
        if (!StringUtils.hasText(sourceSite2)) {
            sourceSite2 = SeerConstant.SITE_SELF;
        }

        Map<String, Object> bodyDataArea = new HashMap<>();
        bodyDataArea.put("source_id", sourceSite2);
        bodyDataArea.put("id", targetSite);
        bodyDataArea.put("task_id", ConnectionUtil.genSeerTaskId());
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_GO_TARGET_3051, new ObjectMapper().writeValueAsString(bodyDataArea));

        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    public SeerDataPacket goTarget3051JackLoad(String sourceSite, String targetSite) throws IOException {
        if (!StringUtils.hasText(targetSite)) {
            throw new BusinessException(999001, "无效的目标站点");
        }
        String sourceSite2 = sourceSite;
        if (!StringUtils.hasText(sourceSite2)) {
            sourceSite2 = SeerConstant.SITE_SELF;
        }
        // TODO 如果已顶升则需要先放下

        Map<String, Object> bodyDataArea = new HashMap<>();
        bodyDataArea.put("source_id", sourceSite2);
        bodyDataArea.put("id", targetSite);
        bodyDataArea.put("task_id", ConnectionUtil.genSeerTaskId());
        bodyDataArea.put("operation", "JackLoad");
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_GO_TARGET_3051, new ObjectMapper().writeValueAsString(bodyDataArea));
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    public SeerDataPacket goTarget3051JackUnload(String sourceSite, String targetSite) throws IOException {
        if (!StringUtils.hasText(targetSite)) {
            throw new BusinessException(999001, "无效的目标站点");
        }
        String sourceSite2 = sourceSite;
        if (!StringUtils.hasText(sourceSite2)) {
            sourceSite2 = SeerConstant.SITE_SELF;
        }
        // TODO 如果已顶升则需要先放下

        Map<String, Object> bodyDataArea = new HashMap<>();
        bodyDataArea.put("source_id", sourceSite2);
        bodyDataArea.put("id", targetSite);
        bodyDataArea.put("task_id", ConnectionUtil.genSeerTaskId());
        bodyDataArea.put("operation", "JackUnload");
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_GO_TARGET_3051, new ObjectMapper().writeValueAsString(bodyDataArea));
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    public SeerDataPacket taskListName3106(String taskListName) throws IOException {
        if (!StringUtils.hasText(taskListName)) {
            throw new BusinessException(999001, "无效的任务链名称");
        }

        Map<String, Object> bodyDataArea = new HashMap<>();
        bodyDataArea.put("name", taskListName);
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_TASK_LIST_NAME, new ObjectMapper().writeValueAsString(bodyDataArea));
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }
}